package com.dsj.dao;

import com.dsj.entity.OrderEntity;
import com.dsj.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    private Connection connection = null;  // 数据库连接对象
    private PreparedStatement ps = null;  // 预处理语句对象
    private DBConnection jdbc = null;  // 数据库连接工具对象

    /**
     * OrderDao类的构造方法
     */
    public OrderDao() {
        jdbc = new DBConnection();  // 创建数据库连接对象实例
        connection = jdbc.connetction;  // 获取数据库连接对象的引用
    }

    /**
     * 新增订单
     *
     * @param form 订单实体类
     * @return
     */
    public boolean insertOrder(OrderEntity form) {
        try {
            String sql = "insert into tb_order(order_id, name, real_name, address, mobile, total_price, delivery_method, memo, delivery_sign, payment_mode, create_time) values(?,?,?,?,?,?,?,?,?,?,?)";
            ps = connection.prepareStatement(sql);
            ps.setString(1, form.getOrderId());
            ps.setString(2, form.getName());
            ps.setString(3, form.getRealName());
            ps.setString(4, form.getAddress());
            ps.setString(5, form.getMobile());
            ps.setFloat(6, form.getTotalPrice());
            ps.setString(7, form.getDeliveryMethod());
            ps.setString(8, form.getMemo());
            ps.setBoolean(9, form.isDeliverySign());
            ps.setString(10, form.getPaymentMode());
            ps.setString(11, form.getCreateTime());
            ps.executeUpdate();
            ps.close();
            return true;
        } catch (Exception e) {
            System.out.println("数据库访问失败!");
            return false;
        }
    }

    /**
     * 前台会员订单查询（按会员名查询）
     * 输入：会员名
     * 输出：该会员对应的所有订单集合List[e]，其中e是OrderEntity对象
     */
    public List<OrderEntity> selectOrderByName(String name) {
        List<OrderEntity> list = new ArrayList<>();
        try {
            String sql = "select * from tb_order where name =?";

            ps = connection.prepareStatement(sql);
            ps.setString(1, name);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setOrderId(rs.getString("order_id"));
                orderEntity.setName(rs.getString("name"));
                orderEntity.setRealName(rs.getString("real_name"));
                orderEntity.setAddress(rs.getString("address"));
                orderEntity.setMobile(rs.getString("mobile"));
                orderEntity.setTotalPrice(rs.getFloat("total_price"));
                orderEntity.setDeliveryMethod(rs.getString("delivery_method"));
                orderEntity.setMemo(rs.getString("memo"));
                orderEntity.setDeliverySign(rs.getBoolean("delivery_sign"));
                orderEntity.setPaymentMode(rs.getString("payment_mode"));
                orderEntity.setCreateTime(rs.getString("create_time"));
                list.add(orderEntity);
            }
        } catch (Exception e) {
            System.out.println("数据库访问失败!");
        }
        return list;
    }

    /**
     * 根据订单id查询订单信息
     * @param orderId 订单id
     * @return 订单实体类
     */
    public OrderEntity selectOrderByOrderId(String orderId) {
        OrderEntity orderEntity = new OrderEntity();
        try {
            String sql = "select * from tb_order where order_id =?";
            ps = connection.prepareStatement(sql);
            ps.setString(1, orderId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                orderEntity.setOrderId(rs.getString("order_id"));
                orderEntity.setName(rs.getString("name"));
                orderEntity.setRealName(rs.getString("real_name"));
                orderEntity.setAddress(rs.getString("address"));
                orderEntity.setMobile(rs.getString("mobile"));
                orderEntity.setTotalPrice(rs.getFloat("total_price"));
                orderEntity.setDeliveryMethod(rs.getString("delivery_method"));
                orderEntity.setMemo(rs.getString("memo"));
                orderEntity.setDeliverySign(rs.getBoolean("delivery_sign"));
                orderEntity.setPaymentMode(rs.getString("payment_mode"));
                orderEntity.setCreateTime(rs.getString("create_time"));
            }
        } catch (Exception e) {
            System.out.println("数据库访问失败!");
        }
        return orderEntity;
    }

    /**
     * 前台会员订单查询（按会员名及发货状态查询）
     * 输入：会员名，是否签收
     * 输出：该会员对应的所有满足条件的订单集合List[e]，其中e是OrderEntity对象
     */
    public List<OrderEntity> selectOrderByNameAndDeliverySign(String name, boolean deliverySign) {
        List<OrderEntity> list = new ArrayList<>();
        try {
            String sql = "select * from tb_order where name =? and delivery_sign =? order by order_id DESC ";

            ps = connection.prepareStatement(sql);
            ps.setString(1, name);
            ps.setBoolean(2, deliverySign);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setOrderId(rs.getString("order_id"));
                orderEntity.setName(rs.getString("name"));
                orderEntity.setRealName(rs.getString("real_name"));
                orderEntity.setAddress(rs.getString("address"));
                orderEntity.setMobile(rs.getString("mobile"));
                orderEntity.setTotalPrice(rs.getFloat("total_price"));
                orderEntity.setDeliveryMethod(rs.getString("delivery_method"));
                orderEntity.setMemo(rs.getString("memo"));
                orderEntity.setDeliverySign(rs.getBoolean("delivery_sign"));
                orderEntity.setPaymentMode(rs.getString("payment_mode"));
                orderEntity.setCreateTime(rs.getString("create_time"));
                list.add(orderEntity);
            }
        } catch (Exception e) {
            System.out.println("数据库访问失败!");
        }
        return list;
    }

}
